package cn.edu.gdu.quizpractice.service.impl;

import cn.edu.gdu.quizpractice.dao.ChoiceQuestionDao;
import cn.edu.gdu.quizpractice.entity.ChoiceQuestion;
import cn.edu.gdu.quizpractice.entity.PageBean;
import cn.edu.gdu.quizpractice.service.IChoiceQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Aubery
 * @Date: 2025-01-22-16:27
 * @Description:
 */


@Service
public class ChoiceQuestionServiceImpl extends ServiceImpl<ChoiceQuestionDao, ChoiceQuestion> implements IChoiceQuestionService {

    @Autowired
    @Qualifier("choiceQuestionDao")
    private ChoiceQuestionDao choiceQuestionDao;

    @Override
    public List<ChoiceQuestion> findByCategory_Id(Integer categoryId) {
        return choiceQuestionDao.findByCategory_Id(categoryId);
    }

    @Override
    public List<ChoiceQuestion> findByDifficulty(Integer difficulty) {
        return choiceQuestionDao.findByDifficulty(difficulty);
    }


    @Override
    public List<ChoiceQuestion> findAll() {
        return choiceQuestionDao.findAll();
    }

    @Override
    public void saveQuestionsFromExcel(List<ChoiceQuestion> questions) {
        saveBatch(questions);
    }

    @Override
    public PageBean<ChoiceQuestion> findByPage(Integer pageNum, Integer pageSize, String categoryName) {
        PageBean<ChoiceQuestion> pb = new PageBean<>();
        //开启PageHelper分页查询
        PageHelper.startPage(pageNum, pageSize);
        if (categoryName != null && !categoryName.equals("")) {
            List<ChoiceQuestion> byCategoryName = choiceQuestionDao.findByCategoryName(categoryName);

            Page<ChoiceQuestion> p = (Page<ChoiceQuestion>) byCategoryName;

            //把数据填充到PageBean中
            pb.setTotal(p.getTotal());
            pb.setItems(p.getResult());
            return pb;
        } else {
            List<ChoiceQuestion> all = choiceQuestionDao.findAll();
            Page<ChoiceQuestion> p = (Page<ChoiceQuestion>) all;
            pb.setTotal(p.getTotal());
            pb.setItems(p.getResult());
            return pb;
        }
    }

    @Override
    public boolean updateQuestion(ChoiceQuestion question) {
        log.debug("更新问题题目参数:" + question.getBName());
        return choiceQuestionDao.updateQuestion(question);
    }

    @Override
    public boolean addQuestion(ChoiceQuestion question) {
        return choiceQuestionDao.insert(question) > 0;
    }

    @Override
    public boolean deleteByBankId(Integer bankId) {
        return choiceQuestionDao.deleteByBankId(bankId);
    }

    @Override
    public int deleteQuestionById(Integer id) {
        return choiceQuestionDao.deleteById(id);
    }

}
